package chat;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.TextArea;
import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.ScrollPaneConstants;

import custom.tree.EmployeesSearch;
import file.FileTransfer;

import java.awt.Button;
import java.awt.Label;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import java.awt.Color;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.border.TitledBorder;

import main.Main;
import voSetting.MessageVO;
import voSetting.StateSetVO;

public class Note extends JFrame {
	Main main;
	boolean maximum = false;
	Point point = new Point();  
	boolean resizing = false; 
   EmployeesSearch em_f;
   FileTransfer file_f;
   Vector <String> v_note = new Vector<String>();   //  
   
   
   private JPanel contentPane;
   private JPanel panel;
   private JPanel panel_1;
   private JScrollPane scrollPane;
   private JLabel lblNewLabel;
   private JButton btnNewButton;
   public JLabel lb_name_to;
   private JButton btnNewButton_1;
   private TextArea textArea;
   private JButton button;
   private JLabel lblNewLabel_2;
   private JPanel panel_2;
   private JPanel panel_3;
   private JPanel panel_4;
   private JButton button_1;
   private JButton button_2;
   private JButton button_3;
   private JLabel lblNewLabel_1;

   /**
    * Launch the application.
    */
   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               Note frame = new Note();
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }

   /**
    * Create the frame.
    */
   public Note() {
	   setUndecorated(true);
      setTitle("쪽지 보내기");
      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      setBounds(100, 100, 470, 300);
      setMinimumSize(new Dimension(470, 300));
      contentPane = new JPanel();
      contentPane.setBorder(null);
      setContentPane(contentPane);
      contentPane.setLayout(new BorderLayout(0, 0));
      contentPane.add(getPanel(), BorderLayout.NORTH);
      contentPane.add(getPanel_1(), BorderLayout.SOUTH);
      contentPane.add(getScrollPane_1(), BorderLayout.CENTER);
      
		addMouseListener(new MouseAdapter() {  
		      public void mousePressed(MouseEvent e) {  
		        resizing = getCursor().equals(Cursor.getDefaultCursor())? false:true;  
		        if(!e.isMetaDown()){  
		          point.x = e.getX();  
		          point.y = e.getY();  
		        }  
		      }  
		    });  
		addMouseMotionListener(new MouseMotionAdapter() {  
		      public void mouseDragged(MouseEvent e) {  
		        if(resizing){  
		          Point pt = e.getPoint(); 
		          setSize(getWidth()+pt.x - point.x, getHeight()+pt.y-point.y);  
		          point.x = pt.x;  
		          point.y = pt.y;
		        }  
		        else if(!e.isMetaDown()){  
		        Point p = getLocation();  
		        setLocation(p.x + e.getX() - point.x,  
		            p.y + e.getY() - point.y);  
		        }  
		      }  
		      public void mouseMoved(MouseEvent me){  
		        Point cursorLocation = me.getPoint();  
		        int xPos = cursorLocation.x;  
		        int yPos = cursorLocation.y;  
		        if(xPos < 5) setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));  
		        else if(xPos > getContentPane().getWidth()-5) setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));  
		        else if(yPos < 5) setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));  
		        else if(yPos > getContentPane().getHeight()-5) setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));  
		        else if(getCursor().equals(Cursor.getDefaultCursor()) == false) setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));  
		      }  
		    });
   }
   
   public void setV_notter(Vector v, Main main){   // 메인에서 그룹채팅의 벡터를 받음 
	      this.main = main;
	      Iterator<String> iter = v.iterator();
	      v_note.clear();
	      while(iter.hasNext()){
	         String name = iter.next();
	         v_note.add(name);      
	      }   
	      for(int i=0; i<v_note.size(); i++){
	         System.out.println("아이디 : " + v_note.get(i));         
	      }   
	   }
   
   private JPanel getPanel() {
      if (panel == null) {
         panel = new JPanel();
         panel.setBackground(new Color(152,191,218));
         panel.setBorder(new EmptyBorder(5, 5, 5, 5));
         panel.setLayout(new BorderLayout(0, 0));
         panel.add(getPanel_2(), BorderLayout.NORTH);
         panel.add(getPanel_3(), BorderLayout.SOUTH);
      }
      return panel;
   }
   private JPanel getPanel_1() {
      if (panel_1 == null) {
         panel_1 = new JPanel();
         panel_1.setBackground(new Color(152,191,218));
         panel_1.setBorder(new EmptyBorder(5, 5, 5, 5));
         panel_1.setLayout(new BorderLayout(0, 0));
         panel_1.add(getBtnNewButton(), BorderLayout.EAST);
         panel_1.add(getButton(), BorderLayout.WEST);
         panel_1.add(getLblNewLabel_2(), BorderLayout.CENTER);
      }
      return panel_1;
   }
   private JScrollPane getScrollPane_1() {
      if (scrollPane == null) {
         scrollPane = new JScrollPane();
         scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
         scrollPane.setViewportView(getTextArea());
      }
      return scrollPane;
   }
   private JLabel getLblNewLabel() {
      if (lblNewLabel == null) {
         lblNewLabel = new JLabel("받는 사람");
         lblNewLabel.setOpaque(true);
         lblNewLabel.setBackground(new Color(152,191,218));
         lblNewLabel.setFont(new Font("맑은 고딕", Font.BOLD, 12));
      }
      return lblNewLabel;
   }
   private JButton getBtnNewButton() {
      if (btnNewButton == null) {
         btnNewButton = new JButton("쪽지 전송");
         btnNewButton.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
         btnNewButton.setBackground(new Color(152,191,218));
         btnNewButton.setFont(new Font("맑은 고딕", Font.BOLD, 12));
         btnNewButton.addActionListener(new ActionListener() {

             @Override
             public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                
                ArrayList<MessageVO> al = new ArrayList<MessageVO>();
                
                try {
                   for(int i = 0; i< v_note.size(); i++){
                      
                      MessageVO mvo = new MessageVO();
                      mvo.setMsg_from(Note.this.main.getClientThread().getSmvo().getM_id());
                      mvo.setMsg_context(textArea.getText().toString());
                      
                      if(textArea.getText().length() > 10){
                         String title = textArea.getText().substring(0, 10);
                         mvo.setMsg_title(title);
                      }
                      else{
                         mvo.setMsg_title(textArea.getText().toString());
                      }
                      
                      mvo.setMsg_to(v_note.get(i));
                      
                      al.add(mvo);
                      System.out.println("설마 출력 : " + i);
                   }
                   
                   StateSetVO ssvo = new StateSetVO();
                   ssvo.setVo_type(3);
                   ssvo.setVo(al);
                   Note.this.main.getClientThread().getOos().writeObject(ssvo);
                   Note.this.main.getClientThread().getOos().flush();
                   dispose();
                } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                }
             }
                
             });
      }
      return btnNewButton;
   }
   private JLabel getLb_name_to() {
      if (lb_name_to == null) {
         lb_name_to = new JLabel("받으실 분을 선택해주세요.");
         lb_name_to.setOpaque(true);
         lb_name_to.setBackground(new Color(152,191,218));
         lb_name_to.setHorizontalAlignment(SwingConstants.CENTER);
         lb_name_to.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
      }
      return lb_name_to;
   }
   private JButton getBtnNewButton_1() {
      if (btnNewButton_1 == null) {
         btnNewButton_1 = new JButton("직원 찾기 ");
         btnNewButton_1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
         btnNewButton_1.setBackground(new Color(152,191,218));
         btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
               em_f = new EmployeesSearch();
               em_f.addEmployeeFrame(1, Note.this);
               em_f.setVisible(true);               
            }
         });
         btnNewButton_1.setToolTipText("쪽지를 받으실 분을 선택해주세요.");
         btnNewButton_1.setFont(new Font("맑은 고딕", Font.BOLD, 12));
      }
      return btnNewButton_1;
   }
   private TextArea getTextArea() {
      if (textArea == null) {
         textArea = new TextArea();
         textArea.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
      }
      return textArea;
   }
   private JButton getButton() {
      if (button == null) {
         button = new JButton("파일 전송");
         button.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
         button.setBackground(new Color(152,191,218));
         button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               file_f = new FileTransfer();
               file_f.lb_name_to.setText(lb_name_to.getText());
               file_f.setVisible(true);
            }
         });
         button.setFont(new Font("맑은 고딕", Font.BOLD, 12));
      }
      return button;
   }
   private JLabel getLblNewLabel_2() {
      if (lblNewLabel_2 == null) {
         lblNewLabel_2 = new JLabel("");
         lblNewLabel_2.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
      }
      return lblNewLabel_2;
   }
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setOpaque(false);
			panel_2.setLayout(new BorderLayout(0, 0));
			panel_2.add(getPanel_4(), BorderLayout.EAST);
			panel_2.add(getLblNewLabel_1(), BorderLayout.WEST);
		}
		return panel_2;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setLayout(new BorderLayout(0, 0));
			panel_3.add(getLblNewLabel(), BorderLayout.WEST);
			panel_3.add(getLb_name_to(), BorderLayout.CENTER);
			panel_3.add(getBtnNewButton_1(), BorderLayout.EAST);
		}
		return panel_3;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setBackground(new Color(152,191,218));
			panel_4.add(getButton_2());
			panel_4.add(getButton_1());
			panel_4.add(getButton_3());
		}
		return panel_4;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(maximum !=true){
						setExtendedState(JFrame.MAXIMIZED_BOTH);	
						button_1.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\system\\initsize.png"));
						maximum = true;
					}else{
						setBounds(100, 100, 470, 300);
						button_1.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\system\\maximum.png"));
						maximum = false;
					}
					
				}
			});
			button_1.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\system\\maximum.png"));
			button_1.setBorder(null);
			button_1.setBackground(new Color(152,191,218));
		}
		return button_1;
	}
	private JButton getButton_2() {
		if (button_2 == null) {
			button_2 = new JButton("");
			button_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setState(JFrame.ICONIFIED);	
				}
			});
			button_2.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\system\\minimum.png"));
			button_2.setBorder(null);
			button_2.setBackground(new Color(152,191,218));
		}
		return button_2;
	}
	private JButton getButton_3() {
		if (button_3 == null) {
			button_3 = new JButton("");
			button_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
			button_3.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\system\\close.png"));
			button_3.setBorder(null);
			button_3.setBackground(new Color(152,191,218));
		}
		return button_3;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("MESSANGE");
			lblNewLabel_1.setForeground(Color.WHITE);
			lblNewLabel_1.setFont(new Font("맑은 고딕", Font.BOLD | Font.ITALIC, 12));
		}
		return lblNewLabel_1;
	}
}